Home:ALL Converter>How to implement interface segregation when creating a Windows shared library in C++

How to implement interface segregation when creating a Windows shared library in C++

Ask Time:2018-04-05T17:34:35         Author:Dan

Json Formatter

I have a class implemented in C++. I want to create a shared library that exports that class, but only certain methods. For example, I want to apply internal linkage to all private members.

I have read the problems that anoymous namespaces can cause, so I have discarded that option. Instead, I want to try to use interface segregation, but I don't know how to implement it in a shared library. Let me explain:

Suppose you have the following code:

// MyLib.h
class MyLib {
public:
   int fun1() { return _i; }
   double fun2() { return _d; }
   bool fun3() { return _b; }

private:
   int _i;
   double _d;
   bool _b;
};

I would like to create a shared library that exports the symbol MyLib and only one of its methods, for example fun1.

I can't just mark MyLib as dllexport, since that will export all the class' symbols, including private members. So I want to create an interface with only one method and export it:

class __declspec(dllexport)  IMyLib {
   virtual int fun1() = 0;
};

class MyLib : public IMyLib {
   // Same as above...
};

This schema is explained in the following post: How do I export class functions, but not the entire class in a DLL

Now, my problem is that I don't understand, in the case of being using MyLib.dll, how could you instantiate an IMyLib object, since the class that implements it is hidden (internal linkage), so you can't access it from a shared library.

Author:Dan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49668945/how-to-implement-interface-segregation-when-creating-a-windows-shared-library-in
yy